home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / surfmodl / surfm203.arc / SURFSRC.ARC / READFILE.INC < prev    next >
Text File  |  1987-01-12  |  10KB  |  264 lines

  1. procedure OPENFILE (var Filename: text80; var Infile: text);
  2. { Open a file with error checking. Prompt for new one if file not found }
  3.  
  4. begin   Fileread := FALSE;
  5.   while (NOT Fileread) do begin
  6.     assign (Infile, Filename);
  7.     {$I-}
  8.     reset (Infile);
  9.     {$I+}
  10.     if (ioresult <> 0) then begin
  11.       writeln ('Error: file ',Filename,' does not exist.');
  12.       write ('Enter new file name (or <enter> to exit): ');
  13.       readln (Filename);
  14.       if (Filename = '') then
  15.         halt;
  16.     end else
  17.       Fileread := TRUE;
  18.   end;
  19. end;   { procedure OPENFILE }
  20.  
  21.  
  22.  
  23. procedure READFILE (Filename: text80);
  24. { read the input data from the file }
  25.  
  26. var
  27.   Version: integer;       { used for multiple version input flag (only 4 now) }
  28.   j: integer;             { counter for looping and reading into arrays}
  29.   Infile: text;           { file to read}
  30.   Realvar: vartype;       { temporary array for storage of line input }
  31.   Num: integer;           { number of inputted values on the line }
  32.   Comment: text80;        { comment at end of line }
  33.   Line_num: integer;      { line number in input file }
  34.   Nvread: integer;        { #vertices read so far in this surface }
  35.   Vert: integer;          { vertex # }
  36.   Nscript: integer;       { #script inputs }
  37.   Cmmd: integer;          { script command number }
  38.   Mat: integer;           { material # }
  39.   Node: integer;          { node # }
  40.   Surf: integer;          { surface # }
  41.   Connection: integer;    { next connection number on surface }
  42.  
  43. begin
  44. clrscr;
  45. writeln ('Reading data file . . .');
  46. {$ifdef BIGMEM}
  47.   with ptra^ do with ptrb^ do with ptrc^ do with ptrg^ do
  48.     with ptrh^ do with ptri^ do
  49.       begin {with}
  50. {$endif}
  51.  
  52.   openfile (Filename, Infile);
  53.   readln (Infile, Flpurpose);
  54.   Line_num := 2;
  55.   Num := inreal (Infile, Realvar, Comment, Line_num, FALSE);
  56.   if (Num <> 1) then begin
  57.     writeln ('Bad input: Reading version number.');
  58.     close (Infile);
  59.     halt;
  60.   end;
  61.   Version := round(Realvar[1]);
  62.   if (Version = 1) then begin
  63.     Line_num := Line_num + 1;
  64.     Num := inreal (Infile, Realvar, Comment, Line_num, FALSE);
  65.     if (Num <> 4) then begin
  66.       writeln ('Bad input: Reading #nodes, #surfaces, Maxvert and #materials',
  67.           ' (line ',Line_num,')');
  68.       close (Infile);
  69.       halt;
  70.     end;
  71.     Nnodes := round(Realvar[1]);
  72.     Nsurf := round(Realvar[2]);
  73.     Maxvert := round(Realvar[3]);
  74.     Nmatl := round(Realvar[4]);
  75.     Nscript := 0;
  76.     Nsides := 1;
  77.   end else if (Version = 2) then begin
  78.     Line_num := Line_num + 1;
  79.     Num := inreal (Infile, Realvar, Comment, Line_num, FALSE);
  80.     if (Num <> 6) then begin
  81.       writeln ('Bad input: Reading #matl, #nodes, #surf, #script, Maxvert,',
  82.           ' #sides (line ',Line_num,')');
  83.       close (Infile);
  84.       halt;
  85.     end;
  86.     Nmatl := round(Realvar[1]);
  87.     Nnodes := round(Realvar[2]);
  88.     Nsurf := round(Realvar[3]);
  89.     Nscript := round(Realvar[4]);
  90.     Maxvert := round(Realvar[5]);
  91.     Nsides := round(Realvar[6]);
  92.   end else if (Version = 3) or (Version = 4) then begin
  93.     Line_num := Line_num + 1;
  94.     Num := inreal (Infile, Realvar, Comment, Line_num, FALSE);
  95.     if (Num <> 5) then begin
  96.       writeln ('Bad input: Reading #matl, #nodes, #surf, Maxvert,',
  97.           ' #sides (line ',Line_num,')');
  98.       close (Infile);
  99.       halt;
  100.     end;
  101.     Nmatl := round(Realvar[1]);
  102.     Nnodes := round(Realvar[2]);
  103.     Nsurf := round(Realvar[3]);
  104.     Maxvert := round(Realvar[4]);
  105.     Nsides := round(Realvar[5]);
  106.   end else begin
  107.     writeln('Wrong data input version number specified');
  108.     close (Infile);
  109.     halt;
  110.   end;
  111.  
  112.   if (Nnodes<=MAXNODES) and (Nsurf<=MAXSURF) and (Nmatl<=MAXMATL) and
  113.        (Maxvert*Nsurf<=MAXCONNECT) and (Nsides<=2) and (Nnodes>0) and
  114.        (Nsurf>0) and (Nmatl>0) then begin
  115.     for Mat := 1 to Nmatl do begin
  116.         Line_num := Line_num + 1;
  117.         Num := inreal (Infile, Realvar, Comment, Line_num, FALSE);
  118.         if (Version <= 2) then begin
  119.           if (Num <> 3) then begin
  120.             writeln ('Bad input: Reading data for material #',Mat,' (line ',
  121.                 Line_num,')');
  122.             close (Infile);
  123.             halt;
  124.           end;
  125.           R1[Mat] := Realvar[1];
  126.           R2[Mat] := Realvar[2];
  127.           R3[Mat] := 0.0;
  128.           Color[Mat] := round(Realvar[3]);
  129.           Ambient[Mat] := 0.1;
  130.         end else if (Version = 3) then begin
  131.           if (Num <> 4) then begin
  132.             writeln ('Bad input: Reading data for material #',Mat,' (line ',
  133.                 Line_num,')');
  134.             close (Infile);
  135.             halt;
  136.           end;
  137.           R1[Mat] := Realvar[1];
  138.           R2[Mat] := Realvar[2];
  139.           R3[Mat] := Realvar[3];
  140.           Color[Mat] := round(Realvar[4]);
  141.           Ambient[Mat] := 0.1;
  142.         end else begin
  143.           if (Num <> 5) then begin
  144.             writeln ('Bad input: Reading data for material #',Mat,' (line ',
  145.                 Line_num,')');
  146.             close (Infile);
  147.             halt;
  148.           end;
  149.           R1[Mat] := Realvar[1];
  150.           R2[Mat] := Realvar[2];
  151.           R3[Mat] := Realvar[3];
  152.           Color[Mat] := round(Realvar[4]);
  153.           Ambient[Mat] := Realvar[5];
  154.         end; { if Version }
  155.       end;  {for Mat}
  156.  
  157.       for Node := 1 to Nnodes do
  158.       begin
  159.            Line_num := Line_num + 1;
  160.            Num := inreal (Infile, Realvar, Comment, Line_num, FALSE);
  161.            if (Num <> 3) then
  162.            begin
  163.                 writeln ('Bad input: Reading data for node #',Node,' (line ',
  164.                 Line_num,')');
  165.                 close (Infile);
  166.                 halt;
  167.            end;
  168.            Xworld[Node] := Realvar[1];
  169.            Yworld[Node] := Realvar[2];
  170.            Zworld[Node] := Realvar[3];
  171.       end; {for Node}
  172.       for Surf := 1 to Nsurf do
  173.       begin
  174.         Line_num := Line_num + 1;
  175.         Num := inreal (Infile, Realvar, Comment, Line_num, FALSE);
  176.         if (Num < 5) then begin
  177.            writeln ('Bad input: Reading data for surface #',Surf,' (line ',
  178.            Line_num,')');
  179.            if (Num > 2) then
  180.               writeln ('Must have at least 3 nodes on a surface!');
  181.            close (Infile);
  182.            halt;
  183.         end;
  184.         Nvert[Surf] := round(Realvar[1]);
  185.         Matl[Surf] := round(Realvar[2]);
  186.         if (Nvert[Surf]<3) or (Nvert[Surf]>Maxvert) or (Nvert[Surf]<Num-2)
  187.            or (Matl[Surf]<1) or (Matl[Surf]>Nmatl) then begin
  188.            writeln ('Error in surface ',Surf,'(line ',Line_num,'): ');
  189.            if (Nvert[Surf] < 3) then
  190.               writeln ('Must have at least 3 nodes per surface')
  191.            else if (Nvert[Surf] > Maxvert) then
  192.              writeln ('#vertices exceeds Maxvert')
  193.            else if (Matl[Surf]<1) or (Matl[Surf]>Nmatl) then
  194.              writeln ('Matl no. not in range 0..Nmatl (',Nmatl,')')
  195.            else
  196.              writeln ('#vertices specified does not match #arguments');
  197.            close (Infile);
  198.            halt;
  199.         end; { if Nvert... }
  200.         Nvread := Num - 2;
  201.         for Vert := 1 to Nvread do begin
  202.           Connection := round(Realvar[Vert+2]);
  203.           if (Connection<1) or (Connection>Nnodes) then begin
  204.             writeln ('Error in surface ',Surf,'(line ',Line_num,'): ');
  205.             writeln ('Connection #,',Vert,' not in range 0..Nnodes (',
  206.               Nnodes,')');
  207.             close (Infile);
  208.              halt;
  209.           end;
  210.           Connect[(Surf-1)*Maxvert+Vert] := Connection;
  211.         end; { for Vert }
  212.         while (Nvread < Nvert[Surf]) do begin
  213.           Line_num := Line_num + 1;
  214.           Num := inreal (Infile, Realvar, Comment, Line_num, FALSE);
  215.           if (Num < 1) or (Nvread + Num > Nvert[Surf]) then begin
  216.             writeln ('Error in surface ',Surf,'(line ',Line_num,'): ');
  217.             if (Num = 0) then writeln ('No data read.')
  218.             else if (Nvread + Num > Nvert[Surf]) then
  219.               writeln ('Too many vertices read.');
  220.             close (Infile);
  221.             halt;
  222.           end; { if Num... }
  223.           Vert := Nvread + 1;
  224.           for j := 1 to Num do begin
  225.             Connection := round(Realvar[j]);
  226.             if (Connection<1) or (Connection>Nnodes) then begin
  227.               writeln ('Error in surface ',Surf,'(line ', Line_num,'): ');
  228.               writeln ('Connection #,',Vert,
  229.                        ' not in range 0..Nnodes (',Nnodes,')');
  230.               close (Infile);
  231.               halt;
  232.             end;
  233.             Connect[(Surf-1)*Maxvert+Vert] := Connection;
  234.             Vert := Vert + 1;
  235.           end;
  236.           Nvread := Nvread + Num;
  237.         end; { while }
  238.       end; { for Surf }
  239.     end
  240.     else begin
  241.       if (Nnodes>MAXNODES) or (Nnodes<1) then
  242.         writeln('Nnodes (',Nnodes,') must be between 1 and ',MAXNODES);
  243.       if (Nsurf>MAXSURF) or (Nsurf<1) then
  244.         writeln('Nsurf (',Nsurf,') must be between 1 and ',MAXSURF);
  245.       if (Nmatl>MAXMATL) or (Nmatl<1) then
  246.         writeln('Nmatl (',Nmatl,') must be between 1 and ',MAXMATL);
  247.       if Maxvert*Nsurf>MAXCONNECT then begin
  248.         writeln('Number of surfaces or max number of vertices too large!');
  249.         writeln('Maxvert (',Maxvert,') * Nsurf (',Nsurf,
  250.                 ') must be smaller than ',MAXCONNECT);
  251.       end;
  252.       if (Nsides<1) or (Nsides>2) then
  253.         writeln('Nsides (',Nsides,') must be either 1 or 2');
  254.       close (Infile);
  255.       halt;
  256.     end; { if Nnodes... }
  257.  
  258.     close (Infile);
  259.     readini (Filename);
  260. {$ifdef BIGMEM}
  261.   end; {with}
  262. {$endif}
  263. end; { procedure READFILE }
  264.